

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>How to set up LLVM-style RTTI for your class hierarchy &mdash; LLVM 3.2 documentation</title>
    
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '3.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="LLVM 3.2 documentation" href="index.html" />
    <link rel="up" title="Programming Documentation" href="programming.html" />
    <link rel="next" title="Subsystem Documentation" href="subsystems.html" />
    <link rel="prev" title="Extending LLVM: Adding instructions, intrinsics, types, etc." href="ExtendingLLVM.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head>
  <body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="subsystems.html" title="Subsystem Documentation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ExtendingLLVM.html" title="Extending LLVM: Adding instructions, intrinsics, types, etc."
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

          <li><a href="programming.html" accesskey="U">Programming Documentation</a> &raquo;</li> 
      </ul>
    </div>

      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">How to set up LLVM-style RTTI for your class hierarchy</a><ul>
<li><a class="reference internal" href="#background">Background</a></li>
<li><a class="reference internal" href="#basic-setup">Basic Setup</a></li>
<li><a class="reference internal" href="#concrete-bases-and-deeper-hierarchies">Concrete Bases and Deeper Hierarchies</a><ul>
<li><a class="reference internal" href="#the-contract-of-classof">The Contract of <tt class="docutils literal"><span class="pre">classof</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#rules-of-thumb">Rules of Thumb</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="ExtendingLLVM.html"
                        title="previous chapter">Extending LLVM: Adding instructions, intrinsics, types, etc.</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="subsystems.html"
                        title="next chapter">Subsystem Documentation</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/HowToSetUpLLVMStyleRTTI.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="how-to-set-up-llvm-style-rtti-for-your-class-hierarchy">
<span id="how-to-set-up-llvm-style-rtti"></span><h1><a class="toc-backref" href="#id1">How to set up LLVM-style RTTI for your class hierarchy</a><a class="headerlink" href="#how-to-set-up-llvm-style-rtti-for-your-class-hierarchy" title="Permalink to this headline">¶</a></h1>
<p><em>Section author: Sean Silva &lt;<a class="reference external" href="mailto:silvas&#37;&#52;&#48;purdue&#46;edu">silvas<span>&#64;</span>purdue<span>&#46;</span>edu</a>&gt;</em></p>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#how-to-set-up-llvm-style-rtti-for-your-class-hierarchy" id="id1">How to set up LLVM-style RTTI for your class hierarchy</a><ul>
<li><a class="reference internal" href="#background" id="id2">Background</a></li>
<li><a class="reference internal" href="#basic-setup" id="id3">Basic Setup</a></li>
<li><a class="reference internal" href="#concrete-bases-and-deeper-hierarchies" id="id4">Concrete Bases and Deeper Hierarchies</a><ul>
<li><a class="reference internal" href="#the-contract-of-classof" id="id5">The Contract of <tt class="docutils literal"><span class="pre">classof</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#rules-of-thumb" id="id6">Rules of Thumb</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="background">
<h2><a class="toc-backref" href="#id2">Background</a><a class="headerlink" href="#background" title="Permalink to this headline">¶</a></h2>
<p>LLVM avoids using C++&#8217;s built in RTTI. Instead, it  pervasively uses its
own hand-rolled form of RTTI which is much more efficient and flexible,
although it requires a bit more work from you as a class author.</p>
<p>A description of how to use LLVM-style RTTI from a client&#8217;s perspective is
given in the <a class="reference external" href="ProgrammersManual.html#isa">Programmer&#8217;s Manual</a>. This
document, in contrast, discusses the steps you need to take as a class
hierarchy author to make LLVM-style RTTI available to your clients.</p>
<p>Before diving in, make sure that you are familiar with the Object Oriented
Programming concept of &#8220;<a class="reference external" href="http://en.wikipedia.org/wiki/Is-a">is-a</a>&#8221;.</p>
</div>
<div class="section" id="basic-setup">
<h2><a class="toc-backref" href="#id3">Basic Setup</a><a class="headerlink" href="#basic-setup" title="Permalink to this headline">¶</a></h2>
<p>This section describes how to set up the most basic form of LLVM-style RTTI
(which is sufficient for 99.9% of the cases). We will set up LLVM-style
RTTI for this class hierarchy:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
  <span class="n">Shape</span><span class="p">()</span> <span class="p">{}</span>
  <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Square</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="kt">double</span> <span class="n">SideLength</span><span class="p">;</span>
<span class="k">public</span><span class="o">:</span>
  <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
  <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Circle</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="kt">double</span> <span class="n">Radius</span><span class="p">;</span>
<span class="k">public</span><span class="o">:</span>
  <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
  <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>The most basic working setup for LLVM-style RTTI requires the following
steps:</p>
<ol class="arabic">
<li><p class="first">In the header where you declare <tt class="docutils literal"><span class="pre">Shape</span></tt>, you will want to <tt class="docutils literal"><span class="pre">#include</span>
<span class="pre">&quot;llvm/Support/Casting.h&quot;</span></tt>, which declares LLVM&#8217;s RTTI templates. That
way your clients don&#8217;t even have to think about it.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#include &quot;llvm/Support/Casting.h&quot;</span>
</pre></div>
</div>
</li>
<li><p class="first">In the base class, introduce an enum which discriminates all of the
different concrete classes in the hierarchy, and stash the enum value
somewhere in the base class.</p>
<p>Here is the code after introducing this change:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
 <span class="k">public</span><span class="o">:</span>
<span class="o">+</span>  <span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast&lt;&gt; et al.)</span>
<span class="o">+</span>  <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
<span class="o">+</span>    <span class="n">SK_Square</span><span class="p">,</span>
<span class="o">+</span>    <span class="n">SK_Circle</span>
<span class="o">+</span>  <span class="p">};</span>
<span class="o">+</span><span class="k">private</span><span class="o">:</span>
<span class="o">+</span>  <span class="k">const</span> <span class="n">ShapeKind</span> <span class="n">Kind</span><span class="p">;</span>
<span class="o">+</span><span class="k">public</span><span class="o">:</span>
<span class="o">+</span>  <span class="n">ShapeKind</span> <span class="n">getKind</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Kind</span><span class="p">;</span> <span class="p">}</span>
<span class="o">+</span>
   <span class="n">Shape</span><span class="p">()</span> <span class="p">{}</span>
   <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
 <span class="p">};</span>
</pre></div>
</div>
<p>You will usually want to keep the <tt class="docutils literal"><span class="pre">Kind</span></tt> member encapsulated and
private, but let the enum <tt class="docutils literal"><span class="pre">ShapeKind</span></tt> be public along with providing a
<tt class="docutils literal"><span class="pre">getKind()</span></tt> method. This is convenient for clients so that they can do
a <tt class="docutils literal"><span class="pre">switch</span></tt> over the enum.</p>
<p>A common naming convention is that these enums are &#8220;kind&#8221;s, to avoid
ambiguity with the words &#8220;type&#8221; or &#8220;class&#8221; which have overloaded meanings
in many contexts within LLVM. Sometimes there will be a natural name for
it, like &#8220;opcode&#8221;. Don&#8217;t bikeshed over this; when in doubt use <tt class="docutils literal"><span class="pre">Kind</span></tt>.</p>
<p>You might wonder why the <tt class="docutils literal"><span class="pre">Kind</span></tt> enum doesn&#8217;t have an entry for
<tt class="docutils literal"><span class="pre">Shape</span></tt>. The reason for this is that since <tt class="docutils literal"><span class="pre">Shape</span></tt> is abstract
(<tt class="docutils literal"><span class="pre">computeArea()</span> <span class="pre">=</span> <span class="pre">0;</span></tt>), you will never actually have non-derived
instances of exactly that class (only subclasses). See <a class="reference internal" href="#concrete-bases-and-deeper-hierarchies">Concrete Bases
and Deeper Hierarchies</a> for information on how to deal with
non-abstract bases. It&#8217;s worth mentioning here that unlike
<tt class="docutils literal"><span class="pre">dynamic_cast&lt;&gt;</span></tt>, LLVM-style RTTI can be used (and is often used) for
classes that don&#8217;t have v-tables.</p>
</li>
<li><p class="first">Next, you need to make sure that the <tt class="docutils literal"><span class="pre">Kind</span></tt> gets initialized to the
value corresponding to the dynamic type of the class. Typically, you will
want to have it be an argument to the constructor of the base class, and
then pass in the respective <tt class="docutils literal"><span class="pre">XXXKind</span></tt> from subclass constructors.</p>
<p>Here is the code after that change:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
 <span class="k">public</span><span class="o">:</span>
   <span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast&lt;&gt; et al.)</span>
   <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
     <span class="n">SK_Square</span><span class="p">,</span>
     <span class="n">SK_Circle</span>
   <span class="p">};</span>
 <span class="k">private</span><span class="o">:</span>
   <span class="k">const</span> <span class="n">ShapeKind</span> <span class="n">Kind</span><span class="p">;</span>
 <span class="k">public</span><span class="o">:</span>
   <span class="n">ShapeKind</span> <span class="n">getKind</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Kind</span><span class="p">;</span> <span class="p">}</span>

<span class="o">-</span>  <span class="n">Shape</span><span class="p">()</span> <span class="p">{}</span>
<span class="o">+</span>  <span class="n">Shape</span><span class="p">(</span><span class="n">ShapeKind</span> <span class="n">K</span><span class="p">)</span> <span class="o">:</span> <span class="n">Kind</span><span class="p">(</span><span class="n">K</span><span class="p">)</span> <span class="p">{}</span>
   <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Square</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">SideLength</span><span class="p">;</span>
 <span class="k">public</span><span class="o">:</span>
<span class="o">-</span>  <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
<span class="o">+</span>  <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Square</span><span class="p">),</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Circle</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">Radius</span><span class="p">;</span>
 <span class="k">public</span><span class="o">:</span>
<span class="o">-</span>  <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
<span class="o">+</span>  <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Circle</span><span class="p">),</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
 <span class="p">};</span>
</pre></div>
</div>
</li>
<li><p class="first">Finally, you need to inform LLVM&#8217;s RTTI templates how to dynamically
determine the type of a class (i.e. whether the <tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt>/<tt class="docutils literal"><span class="pre">dyn_cast&lt;&gt;</span></tt>
should succeed). The default &#8220;99.9% of use cases&#8221; way to accomplish this
is through a small static member function <tt class="docutils literal"><span class="pre">classof</span></tt>. In order to have
proper context for an explanation, we will display this code first, and
then below describe each part:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
 <span class="k">public</span><span class="o">:</span>
   <span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast&lt;&gt; et al.)</span>
   <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
     <span class="n">SK_Square</span><span class="p">,</span>
     <span class="n">SK_Circle</span>
   <span class="p">};</span>
 <span class="k">private</span><span class="o">:</span>
   <span class="k">const</span> <span class="n">ShapeKind</span> <span class="n">Kind</span><span class="p">;</span>
 <span class="k">public</span><span class="o">:</span>
   <span class="n">ShapeKind</span> <span class="n">getKind</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Kind</span><span class="p">;</span> <span class="p">}</span>

   <span class="n">Shape</span><span class="p">(</span><span class="n">ShapeKind</span> <span class="n">K</span><span class="p">)</span> <span class="o">:</span> <span class="n">Kind</span><span class="p">(</span><span class="n">K</span><span class="p">)</span> <span class="p">{}</span>
   <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Square</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">SideLength</span><span class="p">;</span>
 <span class="k">public</span><span class="o">:</span>
   <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Square</span><span class="p">),</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="o">+</span>
<span class="o">+</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">+</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">==</span> <span class="n">SK_Square</span><span class="p">;</span>
<span class="o">+</span>  <span class="p">}</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Circle</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">Radius</span><span class="p">;</span>
 <span class="k">public</span><span class="o">:</span>
   <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Circle</span><span class="p">),</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="o">+</span>
<span class="o">+</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">+</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">==</span> <span class="n">SK_Circle</span><span class="p">;</span>
<span class="o">+</span>  <span class="p">}</span>
 <span class="p">};</span>
</pre></div>
</div>
<p>The job of <tt class="docutils literal"><span class="pre">classof</span></tt> is to dynamically determine whether an object of
a base class is in fact of a particular derived class.  In order to
downcast a type <tt class="docutils literal"><span class="pre">Base</span></tt> to a type <tt class="docutils literal"><span class="pre">Derived</span></tt>, there needs to be a
<tt class="docutils literal"><span class="pre">classof</span></tt> in <tt class="docutils literal"><span class="pre">Derived</span></tt> which will accept an object of type <tt class="docutils literal"><span class="pre">Base</span></tt>.</p>
<p>To be concrete, consider the following code:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">Shape</span> <span class="o">*</span><span class="n">S</span> <span class="o">=</span> <span class="p">...;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">isa</span><span class="o">&lt;</span><span class="n">Circle</span><span class="o">&gt;</span><span class="p">(</span><span class="n">S</span><span class="p">))</span> <span class="p">{</span>
  <span class="cm">/* do something ... */</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The code of the <tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt> test in this code will eventually boil
down&#8212;after template instantiation and some other machinery&#8212;to a
check roughly like <tt class="docutils literal"><span class="pre">Circle::classof(S)</span></tt>. For more information, see
<a class="reference internal" href="#classof-contract"><em>The Contract of classof</em></a>.</p>
<p>The argument to <tt class="docutils literal"><span class="pre">classof</span></tt> should always be an <em>ancestor</em> class because
the implementation has logic to allow and optimize away
upcasts/up-<tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt>&#8216;s automatically. It is as though every class
<tt class="docutils literal"><span class="pre">Foo</span></tt> automatically has a <tt class="docutils literal"><span class="pre">classof</span></tt> like:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
  <span class="p">[...]</span>
  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span>
  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">T</span> <span class="o">*</span><span class="p">,</span>
                      <span class="o">::</span><span class="n">llvm</span><span class="o">::</span><span class="n">enable_if_c</span><span class="o">&lt;</span>
                        <span class="o">::</span><span class="n">llvm</span><span class="o">::</span><span class="n">is_base_of</span><span class="o">&lt;</span><span class="n">Foo</span><span class="p">,</span> <span class="n">T</span><span class="o">&gt;::</span><span class="n">value</span>
                      <span class="o">&gt;::</span><span class="n">type</span><span class="o">*</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">true</span><span class="p">;</span> <span class="p">}</span>
  <span class="p">[...]</span>
<span class="p">};</span>
</pre></div>
</div>
<p>Note that this is the reason that we did not need to introduce a
<tt class="docutils literal"><span class="pre">classof</span></tt> into <tt class="docutils literal"><span class="pre">Shape</span></tt>: all relevant classes derive from <tt class="docutils literal"><span class="pre">Shape</span></tt>,
and <tt class="docutils literal"><span class="pre">Shape</span></tt> itself is abstract (has no entry in the <tt class="docutils literal"><span class="pre">Kind</span></tt> enum),
so this notional inferred <tt class="docutils literal"><span class="pre">classof</span></tt> is all we need. See <a class="reference internal" href="#concrete-bases-and-deeper-hierarchies">Concrete
Bases and Deeper Hierarchies</a> for more information about how to extend
this example to more general hierarchies.</p>
</li>
</ol>
<p>Although for this small example setting up LLVM-style RTTI seems like a lot
of &#8220;boilerplate&#8221;, if your classes are doing anything interesting then this
will end up being a tiny fraction of the code.</p>
</div>
<div class="section" id="concrete-bases-and-deeper-hierarchies">
<h2><a class="toc-backref" href="#id4">Concrete Bases and Deeper Hierarchies</a><a class="headerlink" href="#concrete-bases-and-deeper-hierarchies" title="Permalink to this headline">¶</a></h2>
<p>For concrete bases (i.e. non-abstract interior nodes of the inheritance
tree), the <tt class="docutils literal"><span class="pre">Kind</span></tt> check inside <tt class="docutils literal"><span class="pre">classof</span></tt> needs to be a bit more
complicated. The situation differs from the example above in that</p>
<ul class="simple">
<li>Since the class is concrete, it must itself have an entry in the <tt class="docutils literal"><span class="pre">Kind</span></tt>
enum because it is possible to have objects with this class as a dynamic
type.</li>
<li>Since the class has children, the check inside <tt class="docutils literal"><span class="pre">classof</span></tt> must take them
into account.</li>
</ul>
<p>Say that <tt class="docutils literal"><span class="pre">SpecialSquare</span></tt> and <tt class="docutils literal"><span class="pre">OtherSpecialSquare</span></tt> derive
from <tt class="docutils literal"><span class="pre">Square</span></tt>, and so <tt class="docutils literal"><span class="pre">ShapeKind</span></tt> becomes:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
   <span class="n">SK_Square</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_SpecialSquare</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_OtherSpecialSquare</span><span class="p">,</span>
   <span class="n">SK_Circle</span>
 <span class="p">}</span>
</pre></div>
</div>
<p>Then in <tt class="docutils literal"><span class="pre">Square</span></tt>, we would need to modify the <tt class="docutils literal"><span class="pre">classof</span></tt> like so:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="o">-</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">-</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">==</span> <span class="n">SK_Square</span><span class="p">;</span>
<span class="o">-</span>  <span class="p">}</span>
<span class="o">+</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">+</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="n">SK_Square</span> <span class="o">&amp;&amp;</span>
<span class="o">+</span>           <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">SK_OtherSpecialSquare</span><span class="p">;</span>
<span class="o">+</span>  <span class="p">}</span>
</pre></div>
</div>
<p>The reason that we need to test a range like this instead of just equality
is that both <tt class="docutils literal"><span class="pre">SpecialSquare</span></tt> and <tt class="docutils literal"><span class="pre">OtherSpecialSquare</span></tt> &#8220;is-a&#8221;
<tt class="docutils literal"><span class="pre">Square</span></tt>, and so <tt class="docutils literal"><span class="pre">classof</span></tt> needs to return <tt class="docutils literal"><span class="pre">true</span></tt> for them.</p>
<p>This approach can be made to scale to arbitrarily deep hierarchies. The
trick is that you arrange the enum values so that they correspond to a
preorder traversal of the class hierarchy tree. With that arrangement, all
subclass tests can be done with two comparisons as shown above. If you just
list the class hierarchy like a list of bullet points, you&#8217;ll get the
ordering right:</p>
<div class="highlight-python"><pre>| Shape
  | Square
    | SpecialSquare
    | OtherSpecialSquare
  | Circle</pre>
</div>
<div class="section" id="the-contract-of-classof">
<span id="classof-contract"></span><h3><a class="toc-backref" href="#id5">The Contract of <tt class="docutils literal"><span class="pre">classof</span></tt></a><a class="headerlink" href="#the-contract-of-classof" title="Permalink to this headline">¶</a></h3>
<p>To be more precise, let <tt class="docutils literal"><span class="pre">classof</span></tt> be inside a class <tt class="docutils literal"><span class="pre">C</span></tt>.  Then the
contract for <tt class="docutils literal"><span class="pre">classof</span></tt> is &#8220;return <tt class="docutils literal"><span class="pre">true</span></tt> if the dynamic type of the
argument is-a <tt class="docutils literal"><span class="pre">C</span></tt>&#8221;.  As long as your implementation fulfills this
contract, you can tweak and optimize it as much as you want.</p>
</div>
</div>
<div class="section" id="rules-of-thumb">
<h2><a class="toc-backref" href="#id6">Rules of Thumb</a><a class="headerlink" href="#rules-of-thumb" title="Permalink to this headline">¶</a></h2>
<ol class="arabic simple">
<li>The <tt class="docutils literal"><span class="pre">Kind</span></tt> enum should have one entry per concrete class, ordered
according to a preorder traversal of the inheritance tree.</li>
<li>The argument to <tt class="docutils literal"><span class="pre">classof</span></tt> should be a <tt class="docutils literal"><span class="pre">const</span> <span class="pre">Base</span> <span class="pre">*</span></tt>, where <tt class="docutils literal"><span class="pre">Base</span></tt>
is some ancestor in the inheritance hierarchy. The argument should
<em>never</em> be a derived class or the class itself: the template machinery
for <tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt> already handles this case and optimizes it.</li>
<li>For each class in the hierarchy that has no children, implement a
<tt class="docutils literal"><span class="pre">classof</span></tt> that checks only against its <tt class="docutils literal"><span class="pre">Kind</span></tt>.</li>
<li>For each class in the hierarchy that has children, implement a
<tt class="docutils literal"><span class="pre">classof</span></tt> that checks a range of the first child&#8217;s <tt class="docutils literal"><span class="pre">Kind</span></tt> and the
last child&#8217;s <tt class="docutils literal"><span class="pre">Kind</span></tt>.</li>
</ol>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="subsystems.html" title="Subsystem Documentation"
             >next</a> |</li>
        <li class="right" >
          <a href="ExtendingLLVM.html" title="Extending LLVM: Adding instructions, intrinsics, types, etc."
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

          <li><a href="programming.html" >Programming Documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, LLVM Project.
      Last updated on 2012-12-21.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>